package psn.kiko.constant;

/**
 * 有效权限范围:[0000~1111],[0~15]<br>
 * 无权限：0000 -- 0<br>
 * 具有某一项、两项或三项权限：(0001~0111) -- (0~15)<br>
 * 所有权限[IDUS]：1111 -- 15<br>
 * 基础权限如下：<br>
 * 增[I]：0001 -- 1<br>
 * 删[D]：0010 -- 2<br>
 * 改[U]：0100 -- 4<br>
 * 查[S]：1000 -- 8<br>
 * <i style='color:red;font-size:"20px"'>actualAuthorities</i>：实际权限值，通过实际权限值，计算是否具有某一基础权限的计算方式如下：<br>
 * 1、IDUS & actualAuthorities == I<br>
 * 2、IDUS & actualAuthorities == D<br>
 * 3、IDUS & actualAuthorities == U<br>
 * 4、IDUS & actualAuthorities == S<br>
 */
public class AuthorityConstant{
	/**
	 * 全部权限：增删改查，15
	 */
	public static final short ALL = 0b1111;
	/**
	 * 基础权限：增加,1
	 */
	public static final short INSERTABLE = 0b0001;
	/**
	 * 基础权限:删除,2
	 */
	public static final short DELETABLE = 0b0010;
	/**
	 * 基础权限:修改,4
	 */
	public static final short UPDATABLE = 0b0100;
	/**
	 * 基础权限:查询,8
	 */
	public static final short SELECTABLE = 0b1000;
	/**
	 * 无权限：0
	 */
	public static final short NONE = 0b0000;
	
	/**
	 * 是否具有insert权限
	 *
	 * @param actualAuthorities 实际权限:[0~15]
	 */
	public static boolean hasInsertAuthority(short actualAuthorities){
		return (ALL&actualAuthorities) == INSERTABLE;
	}
	
	/**
	 * 是否具有delete权限
	 *
	 * @param actualAuthorities 实际权限:[0~15]
	 */
	public static boolean hasDeleteAuthority(short actualAuthorities){
		return (ALL&actualAuthorities) == DELETABLE;
	}
	
	/**
	 * 是否具有update权限
	 *
	 * @param actualAuthorities 实际权限:[0~15]
	 */
	public static boolean hasUpdateAuthority(short actualAuthorities){
		return (ALL&actualAuthorities) == UPDATABLE;
	}
	
	/**
	 * 是否具有select权限
	 *
	 * @param actualAuthorities 实际权限:[0~15]
	 */
	public static boolean hasSelectAuthority(short actualAuthorities){
		return (ALL&actualAuthorities) == SELECTABLE;
	}
	
	/**
	 * 是否具有全部权限
	 *
	 * @param actualAuthorities 实际权限:[0~15]
	 */
	public static boolean hasAllAuthority(short actualAuthorities){
		return hasInsertAuthority(actualAuthorities)&
				hasDeleteAuthority(actualAuthorities)&
				hasUpdateAuthority(actualAuthorities)&
				hasSelectAuthority(actualAuthorities);
	}
}
